<template>
    <div class="login_container">
        <div class="login_box">
            <div class="win-scroll">
                <!-- 头像区域 -->
                <div class="avatar_box">
                    <img src="../assets/geely_logo.png" alt="" />
                </div>
                <div class="heading">登录</div>
                <!-- 登录表单区域 -->
                <el-form
                    ref="loginFormRef"
                    :model="loginForm"
                    :rules="loginFormRules"
                    label-width="0px"
                    class="login_form"
                >
                    <!-- 用户名 -->
                    <el-form-item prop="username">
                        <el-input
                            v-model="loginForm.username"
                            prefix-icon="iconfont icon-user"
                            placeholder="用户名"
                        ></el-input>
                    </el-form-item>
                    <!-- 密码 -->
                    <el-form-item prop="password">
                        <el-input
                            v-model="loginForm.password"
                            prefix-icon="iconfont icon-3702mima"
                            placeholder="密码"
                            type="password"
                        ></el-input>
                    </el-form-item>
                    <!-- 图形验证码 -->
                    <el-form-item prop="yzm">
                        <div style="display: flex; align-items: center">
                            <el-input
                                style="width: 250px"
                                v-model="loginForm.captchaCode"
                                placeholder="验证码"
                            ></el-input>
                            <img
                                :src="codeyzm"
                                style="
                  width: 80px;
                  height: 40px;
                  margin-left: 20px;
                  cursor: pointer;
                "
                                alt=""
                                @click="getYzm"
                            />
                        </div>
                    </el-form-item>
                    <!-- 按钮区域 -->
                    <el-form-item class="btns">
                        <el-button type="primary" @click="login"
                            >登录</el-button
                        >
                        <el-button type="info" @click="resetLoginForm"
                            >重置</el-button
                        >
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import { rsaEncrypt } from "../utils/utils.js";
export default {
    data() {
        return {
            // 这是登录表单的数据绑定对象
            loginForm: {
                username: "",
                password: "",
                captchaCode: "",
                captchaKey: ""
            },
            // 这是表单的验证规则对象
            loginFormRules: {
                // 验证用户名是否合法
                username: [
                    {
                        required: true,
                        message: "请输入登录名称",
                        trigger: "blur"
                    },
                    {
                        min: 3,
                        max: 10,
                        message: "长度在 3 到 10 个字符",
                        trigger: "blur"
                    }
                ],
                // 验证密码是否合法
                password: [
                    {
                        required: true,
                        message: "请输入登录密码",
                        trigger: "blur"
                    },
                    {
                        min: 6,
                        max: 15,
                        message: "长度在 6 到 15 个字符",
                        trigger: "blur"
                    }
                ]
            },
            codeyzm: "",
            keyyzm: ""
        };
    },
    methods: {
        // 点击重置按钮，重置登录表单
        resetLoginForm() {
            // console.log(this);
            this.$refs.loginFormRef.resetFields();
        },
        login() {
            this.$refs.loginFormRef.validate(async valid => {
                if (!valid) return;
                this.loginForm.password = rsaEncrypt(this.loginForm.password);
                let param = new FormData();
                for (let key in this.loginForm) {
                    param.append(key, this.loginForm[key]);
                }
                // const { data: res } = await this.$http.post('/operation/xhr/login', param)
                const res = await this.$http.post(
                    "/operation/xhr/login",
                    param
                );
                console.log(res);
                if (res.data.code !== 200)
                    return this.$message.error("登录失败！");
                this.$message.success("登录成功");
                const token = res.headers["x-auth-token"];
                window.sessionStorage.setItem("token", token);
                this.$router.push("/list");
            });
        },
        async getYzm() {
            const res = await this.$http.get("/operation/xhr/captcha/get");
            this.codeyzm = res.data.data.img;
            this.loginForm.captchaKey = res.data.data.key;
        }
    },
    created() {
        this.getYzm();
    }
};
</script>

<style lang="less" scoped>
.login_container {
    background-image: linear-gradient(45deg, #9fbaa8, #31354c);
    height: 100%;
}

.login_box {
    width: 440px;
    //   height: 338px;
    height: 400px;
    background-color: #fff;
    // border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .win-scroll {
        margin: 45.875px 44px;
        .heading {
            line-height: 1.75rem;
            font-size: 1.5rem;
            font-weight: 600;
            margin-top: 15px;
        }
    }
    .avatar_box {
        height: 45px;
        margin-top: -5px;
        box-sizing: border-box;
        transform: translate(0%);
        img {
            height: 16px;
            width: 106px;
        }
    }
}

.login_form {
    position: absolute;
    bottom: 0;
    width: 348px;
    box-sizing: border-box;
}

.btns {
    display: flex;
    justify-content: flex-end;
    .el-button {
        border-radius: 0%;
        width: 108px;
        height: 32px;
        padding: 4px 12px;
        border: none;
    }
}
</style>
